import { } from "https://cdn.bootcss.com/jquery/1.12.4/jquery.min.js";


// 1.用户点击submit按钮直接提交(form+submit+name+action) - 直接完成

// 2.用户名重名检测：如果用户名存在不允许注册。
const $registry = $("#registry");//注册表单
const $tel = $(".reg_tel");//电话输入框
const $pass = $(".reg_pass");//设置密码输入框
const $repass = $(".reg_repass");//确认密码输入框
const $reg_deal = $(".reg_deal");//用户协议复选框

const $tel_hint = $(".reg_tel_hint");//电话判断的提示字
const $pass_hint = $(".reg_pass_hint");//设置密码判断的提示字
const $repass_hint = $(".reg_repass_hint");//确认密码判断的提示字
const $deal_hint = $(".reg_deal_hint");//确认用户协议的提示字

let $telflag = true;
let $passflag = true;
let $dealflag = true;

//输入手机号码
$tel.on("blur", function () {
    if ($tel.val() !== "") {
        let $reg = /^1[35789]\d{9}$/;
        if ($reg.test($tel.val())) {//输入正确的手机号码格式在检测是否重名
            $.ajax({
                type: "post",
                url: "http://10.31.165.61/JS2012/mall360/php/registry.php",
                data: {
                    checkTel: $tel.val()
                },
            }).done(function (data) {
                if (data === "true") {
                    $tel_hint.html("该手机号码已被注册");
                    $tel_hint.css({
                        color: "red"
                    });
                    $telflag = false;
                } else if (data === "false") {

                    $tel_hint.html("√");
                    $tel_hint.css({
                        color: "green"
                    });
                    $telflag = true;
                }
            });
        } else {
            $tel_hint.html("手机号码格式有误");
            $tel_hint.css({ color: "red" })
            $telflag = false;
        }
    } else {
        $tel_hint.html("手机号码不能为空");
        $tel_hint.css({ color: "red" })
        $telflag = false;
    }
});


//设置密码
$pass.on("input", function () {
    if ($pass.val().length >= 8 && $pass.val().length <= 20) {
        var $reg1 = /\d+/;//数字
        var $reg2 = /[a-z]+/;//小写字母
        var $reg3 = /[A-Z]+/;//大写字母
        var $reg4 = /[\W\_]+/;//特殊字符
        var $count = 0;//添加字符的种类

        if ($reg1.test($pass.val())) {
            $count++;
        }
        if ($reg2.test($pass.val())) {
            $count++;
        }
        if ($reg3.test($pass.val())) {
            $count++;
        }
        if ($reg4.test($pass.val())) {
            $count++;
        }

        switch ($count) {
            case 1:
                $pass_hint.html("至少包含数字/字母/字符两种组合，请重新输入");
                $pass_hint.css({ color: "red" });
                $passflag = false;
                break;
            case 2:
            case 3:
                $pass_hint.html("密码安全度 -- 中");
                $pass_hint.css({ color: "orange" });
                $passflag = true;
                break;
            case 4:
                $pass_hint.html("密码安全度 -- 强");
                $pass_hint.css({ color: "green" });
                $passflag = true;
                break;
        }

    } else {
        $pass_hint.html("密码长度有误");
        $pass_hint.css({ color: "red" });
        $passflag = false;
    }
});

$pass.on("blur", function () {
    if ($pass.val() !== "") {
        if ($passflag) {
            $pass_hint.html("√");
            $pass_hint.css({ color: "green" });
        }
    } else {
        $pass_hint.html("密码不能为空");
        $pass_hint.css({ color: "red" });
    }
});


// 确认密码
$repass.on("blur", function () {
    if ($repass.val() !== "") {
        if ($repass.val() === $pass.val()) {
            if ($passflag) {
                $repass_hint.html("√");
                $repass_hint.css({ color: "green" });
                $passflag = true;
            }
        } else {
            $repass_hint.html("密码不一致");
            $repass_hint.css({ color: "red  " });
            $passflag = false;
        }
    } else {
        $repass_hint.html("请确认密码");
        $repass_hint.css({ color: "red" });
        $passflag = false;
    }

});


//阻止浏览器的submit跳转，验证不能通过，不允许提交注册。
$registry.on("submit", function () {
    if ($tel.val() === "") {
        $tel_hint.html("手机号码不能为空");
        $tel_hint.css({ color: "red" });
        $telflag = false;
    }

    if ($pass.val() === "") {
        $pass_hint.html("密码不能为空");
        $pass_hint.css({ color: "red" });
        $passflag = false;
    }

    if ($repass.val() === "") {
        $repass_hint.html("请确认密码");
        $repass_hint.css({ color: "red" });
        $passflag = false;
    }
    if ($reg_deal.prop("checked") != true) {
        $dealflag = false;
        $deal_hint.html("请确认用户协议");
        $deal_hint.css({ color: "red" });
    } else {
        $dealflag = true;
    }

    if (!$telflag || !$passflag || !$dealflag) {
        return false;
    }
});


